<?php
namespace app\web\controller\admin;

use app\admin\library\AdminController;
 
class Param  extends AdminController{
    
    private function getModelId(){
        $model =  $this->model('Model');

        $data = $model->where('type','globalparam')->getRow();

        if ($data){
            return $data['model_id'];
        }else{
            return $model->insert([
                'type' => 'globalparam',
                'title' => '公共参数',
            ]);
        }
    }
    
    public function onIndex(){
        $this->pageTitle = '公共参数';
        $this->pageIcon = 'fa fa-hashtag';
        
        $config = $this->app->config('website',true);
        $config->read();
        
        $this->set('value',$config->toArray());
        $this->set('model_id',$this->getModelId());

        return $this->display();
    }

    public function onSave(){
        $value  = $this->post('value',[]);

        $config = $this->app->config('website',true);

        $config->setData($value);

        if ($config->save()){
            return true;
        }else{
            throw new \Exception('保存失败');
        }
    }
}
